package com.hb.game.pk.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.hb.game.pk.APP;
import com.hb.game.pk.R;
import com.hb.game.pk.adpter.GameListAdapter;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseFragment;
import com.hb.game.pk.beans.IndexGame;
import com.hb.game.pk.beans.NoticeBean;
import com.hb.game.pk.beans.RoomList;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.ui.activity.game.GameRoomActivity;
import com.hb.game.pk.ui.activity.game.GameRuleActivity;
import com.hb.game.pk.ui.activity.game.SortActivity;
import com.hb.game.pk.ui.activity.login.LoginActivity;
import com.hb.game.pk.utils.ScreenUtils;
import com.hb.game.pk.utils.SpUtils;
import com.hb.game.pk.utils.ToastUtils;
import com.hb.game.pk.widgets.titlebar.TitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class GameFragment extends BaseFragment<IndexGame> {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.banner)
    ImageView banner;
    @BindView(R.id.tv_gg)
    TextView tvGG;
    private List<IndexGame.DataBean> data;
    private ArrayList<IndexGame.DataBean> list;
    private MaterialDialog alertDialog;
    private MaterialDialog mDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_game;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return R.id.rv_list;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        super.initView(view, savedInstanceState);
        int screenWidth = ScreenUtils.getScreenWidth(getContext());
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(screenWidth - 60, (int) ((screenWidth - 60) / 3 * 1.5));
        banner.setLayoutParams(params);
        banner.setBackgroundResource(R.mipmap.iv_banner);
    }

    @Override
    protected void onRefreshData() {
        super.onRefreshData();
        call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).gameIndex();
        call.enqueue(new Callback<IndexGame>() {


            @Override
            public void onResponse(Call<IndexGame> call, Response<IndexGame> response) {
                IndexGame body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }
                if (body.getStatus() == 2) {
                    ToastUtils.showToast(body.getInfo());
                    APP.Token = "";
                    APP.uid = "";
                    APP.userInfo = null;
                    SpUtils.remove(mActivity, "uid");
                    SpUtils.remove(mActivity, "toke");
                    SpUtils.remove(mActivity, "userInfo");
                    Intent intent = new Intent(mActivity, LoginActivity.class);
                    startActivity(intent);
                    return;
                }
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                data = body.getData();
                list = new ArrayList<>();
                for (IndexGame.DataBean datatum : GameFragment.this.data) {
                    if (datatum.getGame().equals("saolei")) {
                        list.add(datatum);
                    }
                    if (datatum.getGame().equals("szww")) {
                        list.add(datatum);
                    }
                }
//                IndexGame.DataBean data1 = new IndexGame.DataBean();
//                data1.setGame("saoleity");
//                data1.setTitle("扫雷体验房");
//                data1.setImg("img/game1.png");
//                GameFragment.this.data.add(data1);
                initAdapter();
            }

            @Override
            public void onFailure(Call<IndexGame> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
            }
        });
        Call<NoticeBean> notice = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).notice();
        notice.enqueue(new Callback<NoticeBean>() {
            @Override
            public void onResponse(Call<NoticeBean> call, Response<NoticeBean> response) {
                NoticeBean body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }

                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                List<NoticeBean.DataBean> data = body.getData();
                if (data != null && data.size() > 0) {
                    NoticeBean.DataBean dataBean = data.get(0);
                    View inflate1 = View.inflate(getActivity(), R.layout.game_dialog, null);
                    mDialog = new MaterialDialog.Builder(getActivity()).customView(inflate1, false).build();
                    mDialog.show();
                    TextView textTrue = (TextView) inflate1.findViewById(R.id.text_true);
                    TextView textView = (TextView) inflate1.findViewById(R.id.text_message);
                    textTrue.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mDialog.dismiss();
                        }
                    });
                    textView.setText(dataBean.getDetails());
//                    if (alertDialog == null) {
//                        View inflate = View.inflate(mActivity, R.layout.dialog_alert, null);
//                        TextView tvCansle = (TextView) inflate.findViewById(R.id.tv_cansle);
//                        TextView tvOk = (TextView) inflate.findViewById(R.id.tv_ok);
//                        TextView tvTx = (TextView) inflate.findViewById(R.id.tv_tx);
//                        tvTx.setText(dataBean.getDetails());
//                        tvTx.setTextColor(mActivity.getResources().getColor(R.color.c_000000));
//                        tvCansle.setVisibility(View.GONE);
//                        tvOk.setOnClickListener(new View.OnClickListener() {
//                            @Override
//                            public void onClick(View v) {
//                                if (alertDialog != null && alertDialog.isShowing())
//                                    alertDialog.dismiss();
//                            }
//                        });
//                        alertDialog = new MaterialDialog.Builder(mActivity)
//                                .customView(inflate,false)
//                                .show();
//                    }
                }
            }

            @Override
            public void onFailure(Call<NoticeBean> call, Throwable t) {

            }
        });
    }

    @OnClick(R.id.banner)
    public void onViewClicked() {
        startActivity(new Intent(mActivity, SortActivity.class));
    }

    @Override
    protected void initAdapter() {
        if (mAdapter == null) {
            mAdapter = new GameListAdapter(R.layout.item_game_list);
            mAdapter.setNewData(list);
            mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    IndexGame.DataBean dataBean = list.get(position);
                    String game = dataBean.getGame();
                    Intent intent = new Intent(mActivity, GameRuleActivity.class);
                    intent.putExtra("type", game);
                    startActivity(intent);
                }
            });
            mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    IndexGame.DataBean dataBean = list.get(position);
                    String game = dataBean.getGame();
                    if (game.equals("saoleity")) {
                        Intent intent = new Intent(mActivity, com.hb.game.pk.ui.activity.game.tiyan.SaoLeiChatActivity1.class);
                        RoomList.DataBean tmp = new RoomList.DataBean();
                        tmp.setTitle("扫雷体验房");
                        tmp.setRoom_id("3735284");
                        intent.putExtra("room", new Gson().toJson(tmp));
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(mActivity, GameRoomActivity.class);
                        intent.putExtra("type", game);
                        startActivity(intent);
                    }
                }
            });
        } else {
            mAdapter.setNewData(list);
        }
        super.initAdapter();
    }

    @Override
    public boolean isNeedLoadMore() {
        return false;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

}
